# coding:utf-8

import numpy as np
import pandas as pd

def get_data(path):
    cloumn_names = ['Sample code number','Clump Thickness','Uniformity of Cell Size',
                'Uniformity of Cell Shape','Marginal Adhesion','Single Epithelial Cell size',
                'Bare Nuclei','Bland Chromatin','Normal Nucleoli','Mitoses','Class']

    data = pd.read_csv(path)
    data = data.replace(to_replace="?",value=np.nan)
    data = data.dropna(how='any')
    print(data.shape)

    return data

def split_train_test_data(data):
    from sklearn.model_selection import train_test_split
    X_train_data , X_test_data , y_train_label, y_test_tabel = \
        train_test_split(data[cloumn_names[1:10]],data[column_names[10]],test_size=0.25,random_state=34)

    return X_train_data , X_test_data , y_train_label, y_test_tabel

def standard_data(X_train_data,X_test_data):
    from sklearn.preprocessing import StandardScaler
    ss = StandardScaler()
    X_train_stand = ss.fit_transform(X_train_data)
    X_test_stand = ss.transform(X_test_data)

    return X_train_stand, X_test_stand

def log_model(X_train_stand,X_test_stand,y_train_label):

    from sklearn.linear_model import LogisticRegression

    lr = LogisticRegression()
    lr.fit(X_train_stand,y_train_label)
    lr_y_predict = lr.predict(X_test_stand)
    return lr,lr_y_predict

def evaluate_model(lr,X_test_stand,y_test_tabel,lr_y_predict):
    from sklearn.metrics import classification_report
    print("Accuracy of LR Classifier: ",lr.score(X_test_stand,y_test_tabel))
    print(classification_report(y_test_tabel,lr_y_predict,target_names=['Benign','Malignant']))

def main():
    # url = '''https://archive.ics.uci.edu/ml/machine-learning-databases/breast-cancer-wisconsin
    # /breast-cancer-wisconsin.data'''
    path = r"C:\Users\ljb\Desktop\breast-cancer-wisconsin.data"

    data = get_data(path)

    X_train_data, X_test_data, y_train_label, y_test_tabel = split_train_test_data(data)

    X_train_stand, X_test_stand = standard_data(X_train_data, X_test_data)

    lr,lr_y_predict = log_model(X_train_stand,X_test_stand,y_train_label)

    evaluate_model(lr, X_test_stand, y_test_tabel, lr_y_predict)

if __name__ == '__main__':
    main()